﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBModel;

namespace CV_Projekt
{
    public partial class FormSignUp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBoxCity.Text = "Helsingborg";
                TextBoxCountry.Text = "Sverige";
                TextBoxEmail.Text = "freddie.wahlgren@telia.com";
                TextBoxFirstName.Text = "Freddie";
                TextBoxLastName.Text = "Wahlgren";
                TextBoxSocialSecurityNumber.Text = "198512127576";
                TextBoxPhone.Text = "070-1234567";
                TextBoxPostalCode.Text = "252 20";
                TextBoxStreetAddress.Text = "Södergatan 78";
                TextBoxPassword.Text = "freddie";
                TextBoxUserName.Text = "Freddie";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var user = new Applicant
            {
                City = TextBoxCity.Text,
                Country = TextBoxCountry.Text,
                Email = TextBoxEmail.Text,
                FirstName = TextBoxFirstName.Text,
                LastName = TextBoxLastName.Text,
                SocialSecurityNumber = TextBoxSocialSecurityNumber.Text,
                Phone = TextBoxPhone.Text,
                PostalCode = TextBoxPostalCode.Text,
                Street = TextBoxStreetAddress.Text,
                Password = TextBoxPassword.Text,
                UserName = TextBoxUserName.Text
            };

            List<User> users = DB.Context.Users.Where(u => u.UserName == user.UserName).ToList();

            if (users.Count == 0)
            {
                DB.Context.Users.Add(user);
                var cv = new CurriculumVitae();
                cv.Applicant = user;
                DB.Context.CurriculumVitaes.Add(cv);
                Session["CV"] = cv;
                DB.Context.SaveChanges();
                Session["User"] = UserDB.AuthenticateUser(TextBoxUserName.Text, TextBoxPassword.Text);
                Response.Redirect("RegisterCV.aspx");
            }
            else
            {
                LabelUserExist.Text = "En användare med det användarnamnet finns redan!";
            }
        }
    }
}